package com.bkgy.modular.tbSpcConfig.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.tbSpcConfig.dto.DeleteList;
import com.bkgy.modular.tbSpcConfig.dto.TbSPCDisposeDto;
import com.bkgy.modular.tbSpcConfig.entity.TbSPCDispose;
import com.bkgy.modular.tbSpcConfig.mapper.TbSPCDisposeMapper;
import com.bkgy.modular.tbSpcConfig.service.TbSPCDisposeService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @ClassName tbSPCDisposeServiceImpl
 * @date 2023/3/17 14:31
 */
@Service
public class TbSPCDisposeServiceImpl extends ServiceImpl<TbSPCDisposeMapper, TbSPCDispose> implements TbSPCDisposeService {

    @Resource
    private TbSPCDisposeMapper tbSPCDisposeMapper;

    /**
     * 添加SPC配置业务实现
     * @param tbSPCDisposeDto
     * @return
     */
    @Override
    public ResultData saveSpcData(TbSPCDisposeDto tbSPCDisposeDto) {
        // 判断当前传入对象是否为空
        if (!StringUtils.hasText(tbSPCDisposeDto.getProduction_line()) && !StringUtils.hasText(tbSPCDisposeDto.getParameter_name())) {
            return ResultData.ResultDataFaultSelf("请检查必传参数是否正确",-1);
        }
        // 创建SPC配置对象
        TbSPCDispose tbSPCDispose = new TbSPCDispose();
        // 将接收的参数进行拷贝给tbSPCDispose对象
        BeanUtils.copyProperties(tbSPCDisposeDto,tbSPCDispose);
        // 调用mapper进行添加
        tbSPCDisposeMapper.insert(tbSPCDispose);
        return ResultData.ResultDataFaultSelf("添加数据成功",2002);
    }

    /**
     * 根据分页参数进行查询数据
     * @param jsonObject
     * @return
     */
    @Override
    public ResultData findByData(JSONObject jsonObject) {
        // 接收分页参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        if (pageIndex == null && pageSize == null) {
            return ResultData.ResultDataFaultSelf("检查分页参数是否正确",-1);
        }
        JSONObject condition = jsonObject.getJSONObject("condition");

        List<TbSPCDispose> byData = tbSPCDisposeMapper.findByData((pageIndex - 1) * pageSize, pageIndex * pageSize, condition);

        return ResultData.ResultDataFaultSelf("查询成功",byData);
    }

    /**
     * 修改数据业务实现
     * @param tbSPCDisposeDto
     * @return
     */
    @Override
    public ResultData updateData(TbSPCDisposeDto tbSPCDisposeDto) {
        // 判断当前传入对象是否为空
        if (!StringUtils.hasText(tbSPCDisposeDto.getProduction_line()) && !StringUtils.hasText(tbSPCDisposeDto.getParameter_name())) {
            return ResultData.ResultDataFaultSelf("请检查必传参数是否正确",-1);
        }
        LambdaQueryWrapper<TbSPCDispose> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(item -> item.eq(TbSPCDispose::getProduction_line,tbSPCDisposeDto.getProduction_line())
                .eq(TbSPCDispose::getParameter_name,tbSPCDisposeDto.getParameter_name()));
        tbSPCDisposeMapper.update(tbSPCDisposeDto,queryWrapper);
        return ResultData.ResultDataSuccessSelf("修改成功", 2002);
    }

    @Override
    public ResultData deleteList(JSONObject jsonObject) {
        JSONArray deleteList = jsonObject.getJSONArray("deleteList");
        List<DeleteList> deleteLists = deleteList.toJavaList(DeleteList.class);

        for (DeleteList item2 : deleteLists) {
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("PRODUCTION_LINE",item2.getProduction_line());
            hashMap.put("PARAMETER_NAME",item2.getParameter_name());
            tbSPCDisposeMapper.deleteByMap(hashMap);
        }
        return ResultData.ResultDataSuccessSelf("删除成功", 2002);
    }
}
